package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UploadUtil;
import cn.wolfcode.wolf2w.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/strategy")
@Controller
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IDestinationService destinationService;


    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") StrategyQuery qo){
        //根据请求查询分页数据
        Page<Strategy> page = strategyService.query(qo);
        //加到模型中
        model.addAttribute("page", page);
        return "strategy/list";
    }


    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy){
        //调用服务层方法
        if (StringUtils.hasLength(strategy.getId())){
            //调用更新方法
            strategyService.update(strategy);
        }else {
            //调用保存方法
            strategyService.save(strategy);
        }
        //返回执行成功
        return JsonResult.success();
    }


    @RequestMapping("/input")
    public String input(Model model,String id){

        //添加各种数据
        //查询所有主题,添加进去
        List<StrategyTheme> themes = strategyThemeService.selectAll();
        model.addAttribute("themes", themes);
        //查询出所有分类,添加进去
        List<CatalogVO> catalogs =strategyCatalogService.groupList();
        model.addAttribute("catalogs", catalogs);
        //如果有id查出对象,加进去
        if (StringUtils.hasLength(id)){
            Strategy strategy = strategyService.get(id);
            model.addAttribute("strategy", strategy);
        }
        return "strategy/input";
    }




    @RequestMapping("delete")
    @ResponseBody
    public Object delete(String id){
        //删除
        strategyService.deleteById(id);
        //返回jsonresult
        return JsonResult.success();
    }
    @RequestMapping("changeState")
    @ResponseBody
    public Object changeState(String id,int state){
        //调用攻略服务类进行更改状态
        strategyService.changeState(id,state);
        //返回jsonresult
        return JsonResult.success();
    }



}
